package com.xingyun.kingdee.erp.service.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingyun.kingdee.erp.common.model.param.base.ErpLoginParam;
import com.xingyun.kingdee.erp.common.model.param.opt.*;
import com.xingyun.kingdee.erp.service.common.AccessErpClient;
import com.xingyun.kingdee.erp.service.common.config.ErpUrlConstants;
import com.xingyun.kingdee.erp.service.service.ErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author lingo
 * @version v1.0
 * @date 2020/8/11 16:15
 * @Company www.xyb2b.com
 */
@Service
public class ErpServiceImpl implements ErpService {

    @Autowired
    private AccessErpClient accessErpClient;

    @Override
    public JSONObject save(ErpSaveFormParam erpSaveParam) {
        String url = ErpUrlConstants.saveFormUrl;
        if (Objects.equals(erpSaveParam.getSaveAsDraft(),true)){
            url = ErpUrlConstants.saveDarftFormUrl;
        }
        return accessErpClient.postForJsonObject(url,erpSaveParam);
    }

    @Override
    public JSONObject view(ErpViewParam erpViewParam) {
        return accessErpClient.postForJsonObject(ErpUrlConstants.viewFormUrl, erpViewParam);
    }

    @Override
    public JSONObject delete(ErpDeleteParam erpDeleteParam) {
        return accessErpClient.postForJsonObject(ErpUrlConstants.delUrl, erpDeleteParam);
    }

    @Override
    public List<String> loginToGetCookie(ErpLoginParam erpLoginParam) {
        return accessErpClient.loginToGetCookie(erpLoginParam);
    }

    @Override
    public JSONObject push(ErpPushDownParam erpPushParam) {
        return accessErpClient.postForJsonObject(ErpUrlConstants.pushDownUrl,erpPushParam);
    }

    @Override
    public JSONObject audit(ErpAuditParam erpAuditParam) {
        return accessErpClient.postForJsonObject(ErpUrlConstants.auditFormUrl,erpAuditParam);
    }

    @Override
    public JSONObject unAudit(ErpUnAuditParam erpUnAuditParam) {
        return accessErpClient.postForJsonObject(ErpUrlConstants.unAuditUrl,erpUnAuditParam);
    }

    @Override
    public JSONObject submit(ErpSubmitParam erpSubmitParam) {
        return accessErpClient.postForJsonObject(ErpUrlConstants.submitFormUrl,erpSubmitParam);
    }

    @Override
    public JSONArray billQuery(ErpBillQueryParam erpBillQueryParam) {
        String s = accessErpClient.postForJsonString(ErpUrlConstants.viewListUrl, erpBillQueryParam);
        return JSONArray.parseArray(s);
    }
}
